package cn.Alibaba.user.service.impl;

import cn.Alibaba.basic.constant.VerifyConstants;
import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.util.Md5Utils;
import cn.Alibaba.basic.util.StrUtils;
import cn.Alibaba.user.domain.Logininfo;
import cn.Alibaba.user.domain.User;
import cn.Alibaba.user.dto.UserDto;
import cn.Alibaba.user.mapper.LogininfoMapper;
import cn.Alibaba.user.mapper.UserMapper;
import cn.Alibaba.user.service.IUserService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    public User loadByPhone(String phone) {
        return userMapper.loadByPhone(phone);
    }

    @Override
    public void phoneRegister(UserDto userDto) {



        // 获取手机号码
        String phone = userDto.getPhone();
        // 获取手机验证码
        String phoneCode = userDto.getPhoneCode();
        // 获取手机密码
        String password = userDto.getPassword();
        // 获取确认密码
        String passwordRepeat = userDto.getPasswordRepeat();

        // 校验空值
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(phoneCode)||
        StringUtils.isEmpty(password)||StringUtils.isEmpty(passwordRepeat)){

            throw new BusinessException("参数不能为空！");
        }

        // 校验密码是否一致
        if (!password.trim().equals(passwordRepeat.trim())){

            throw new BusinessException("两次密码不一致！");

        }

        // 校验 验证码是否过期
        Object redisPhoneCode = redisTemplate.opsForValue().get(phone + VerifyConstants.PHONEREGISTER);
        System.out.println(redisPhoneCode);

        if (redisPhoneCode==null){

            throw new BusinessException("手机号验证码失效！");
        }

        // 校验 验证码是否正确
        if(!redisPhoneCode.toString().split(":")[0].equalsIgnoreCase(phoneCode)){

            throw new BusinessException("手机号验证码错误！");

        }


       User user = userDto2User(userDto);

        Logininfo logininfo= user2logininof(user);


        // 添加logininfo信息
        logininfoMapper.add(logininfo);


        // 添加user信息
        user.setLogininfoId(logininfo.getId());

        userMapper.add(user);




    }

    private Logininfo user2logininof(User user) {

        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(user,logininfo);

        logininfo.setType(1);


        return logininfo;

    }

    private User userDto2User(UserDto userDto) {

        User user = new User();

        user.setUsername(userDto.getPhone());
        
        user.setPhone(userDto.getPhone());

        // 设置颜值
        String salt = StrUtils.getComplexRandomString(32);

        user.setSalt(salt);

        // 设置密码
        String md5Password = Md5Utils.encrypByMd5(userDto.getPassword() + salt);

        user.setPassword(md5Password);


        return user;

    }




}
